using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QuanLyKhachHang.DAO
{
	#region using
	using System.Data;
	using QuanLyKhachHang.DTO;
	#endregion
	public class TongHopDAO : AbstractDAO
	{
		public TongHopDAO(String strPath): base(strPath)
		{
		}

		~TongHopDAO()
		{
			base.Dispose();
		}

		public DataSet SelectAll()
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_TongHop_SelectAll";
				base.FillDataSet(ref ds, "TongHop");
				return ds;
			}
			catch(System.Exception e)
			{
				throw new Exception(e.Message, e.InnerException);
			}
			finally
			{
				base.CleanUpObject();
			}
		}

		public DataSet SelectByID(int matonghop)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_TongHop_SelectByID";
				base.InitializeCommand();
				base.AddParameter("@MATONGHOP", SqlDbType.Int, 100, matonghop);
				base.FillDataSet(ref ds, "TongHop");
				return ds;
			}
			catch(System.Exception e)
			{
				throw new Exception(e.Message, e.InnerException);
			}
			finally
			{
				base.CleanUpObject();
			}
		}


		public bool Insert(TongHopDTO obj)
		{
            bool kq;
			try
			{
				base.sql = "usp_TongHop_Insert";
				base.InitializeCommand();
				base.AddParameter("@MAKH", SqlDbType.Int, 100, obj.MAKH);
				base.AddParameter("@THANG", SqlDbType.Int, 100, obj.THANG);
				base.AddParameter("@NAM", SqlDbType.Int, 100, obj.NAM);
                base.AddParameter("@GHICHU", SqlDbType.NVarChar, 100, obj.GHICHU == null ? "" : obj.GHICHU);
                base.AddParameterWithDirection("@MATONGHOP", SqlDbType.BigInt, 100);
                kq = ExecuteStoredProcedure();
                obj.MATONGHOP = int.Parse(base.GetParamValue("@MATONGHOP").ToString());
                return kq;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int matonghop)
		{
			try
			{
				base.sql = "usp_TongHop_Delete";
				base.InitializeCommand();
				base.AddParameter("@MATONGHOP", SqlDbType.Int, 100, matonghop);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public bool Update(TongHopDTO obj)
		{
			try
			{
				base.sql = "usp_TongHop_Update";
				base.InitializeCommand();
				base.AddParameter("@MATONGHOP", SqlDbType.Int, 100, obj.MATONGHOP);
				base.AddParameter("@MAKH", SqlDbType.Int, 100, obj.MAKH);
				base.AddParameter("@THANG", SqlDbType.Int, 100, obj.THANG);
				base.AddParameter("@NAM", SqlDbType.Int, 100, obj.NAM);
                base.AddParameter("@GHICHU", SqlDbType.NVarChar, 100, obj.GHICHU);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public DataSet SelectByMAKH(int makh)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_TongHop_SelectByMAKH";
				base.InitializeCommand();
				base.AddParameter("@MAKH", SqlDbType.Int, 100, makh);
				base.FillDataSet(ref ds, "TongHop");
				return ds;
			}
			catch(System.Exception e)
			{
				throw new Exception(e.Message, e.InnerException);
			}
			finally
			{
				base.CleanUpObject();
			}
		}

        public DataSet SelectByMAKHTHANGNAM(int makh, int thang, int nam)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_TongHop_SelectByMAKHTHANGNAM";
                base.InitializeCommand();
                base.AddParameter("@MAKH", SqlDbType.Int, 100, makh);
                base.AddParameter("@THANG", SqlDbType.Int, 100, thang);
                base.AddParameter("@NAM", SqlDbType.Int, 100, nam);
                base.FillDataSet(ref ds, "TongHop");
                return ds;
            }
            catch (System.Exception e)
            {
                throw new Exception(e.Message, e.InnerException);
            }
            finally
            {
                base.CleanUpObject();
            }
        }


        public DataSet SelectByMADONVI(string madonvi, int thang, int nam)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_TongHop_SelectByMADONVI";
                base.InitializeCommand();
                base.AddParameter("@MADONVI", SqlDbType.NVarChar, 100, madonvi);
                base.AddParameter("@THANG", SqlDbType.Int, 100, thang);
                base.AddParameter("@NAM", SqlDbType.Int, 100, nam);
                base.FillDataSet(ref ds, "TongHop");
                return ds;
            }
            catch (System.Exception e)
            {
                throw new Exception(e.Message, e.InnerException);
            }
            finally
            {
                base.CleanUpObject();
            }
        }

        public DataSet SelectByNAM(int nam)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_TongHop_SelectByNAM";
                base.InitializeCommand();
                base.AddParameter("@NAM", SqlDbType.Int, 100, nam);
                base.FillDataSet(ref ds, "TongHop");
                return ds;
            }
            catch (System.Exception e)
            {
                throw new Exception(e.Message, e.InnerException);
            }
            finally
            {
                base.CleanUpObject();
            }
        }

        public DataSet CheckingDonViHaveAnyKhachHang()
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_CheckingDonViHaveAnyKhachHang";
                base.FillDataSet(ref ds, "TongHop");
                return ds;
            }
            catch (System.Exception e)
            {
                throw new Exception(e.Message, e.InnerException);
            }
            finally
            {
                base.CleanUpObject();
            }
        }
	}
}